{*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2014 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*}
<script>
    {*    var baseDir = '{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}';*}
    var baseDir = "";
    $(function() {
        $(".shelf-list").change(function() {
            var value = $(this).val();
            var id_row = this.id.replace('shelf_', '');
            showBinList(id_row, value);
            showContainerList(id_row);
        });
        $(".bin-list").change(function() {
            var value = $(this).val();
            var id_row = this.id.replace('bin_', '');
            showContainerList(id_row, value);
        });

        $('.check_warehouse').click(function() {
            $('.check_warehouse').filter(':checked').not(this).removeAttr('checked');
        });
    });
    function showBinList(id_row, id_shelf) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'getBinList',
                'id_shelf': id_shelf,
            },
            dataType: 'json',
            success: function(json) {
                $('#bin_' + id_row).empty();
                $('#bin_' + id_row).append($('<option>').text('Select a Bin').attr('value', 0));
                $.each(json, function(i, value) {
                    $('#bin_' + id_row).append($('<option>').text(value.name).attr('value', value.id_bin));
                });

            }
        });
    }
    function showContainerList(id_warehouse, id_bin) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'getContainerList',
                'id_bin': id_bin,
            },
            dataType: 'json',
            success: function(json) {
                $('#container_' + id_warehouse).empty();
                $('#container_' + id_warehouse).append($('<option>').text('Select a Container').attr('value', 0));
                $.each(json, function(i, value) {
                    $('#container_' + id_warehouse).append($('<option>').text(value.name).attr('value', value.id_container));
                });

            }
        });
    }
</script>
<input type="hidden" name="warehouse_loaded" value="1">
{if isset($product->id)}
    <div id="product-warehouses" class="panel product-tab">
        <input type="hidden" name="submitted_tabs[]" value="Warehouses" />
        <h3>{l s='Product location in warehouses'}</h3>
        <div class="row">
            <div class="alert alert-info" style="display:block; position:'auto';">
                <p>{l s='This interface allows you to specify the warehouse in which the product is stocked.'}</p>
                <p>{l s='You can also specify product/product combinations as it relates to warehouse location. '}</p>
            </div>
            <p>{l s='Please choose the warehouses associated with this product. You must also select a default warehouse. '}</p>
        </div>	
        <div class="row">
            <a class="btn btn-link confirm_leave" href="{$link->getAdminLink('AdminWarehouses')|escape:'html':'UTF-8'}&addwarehouse">{l s='Create a new warehouse'} <i class="icon-external-link-sign"></i></a>
        </div>
        <div class="row">
            <div class="panel-group" id="warehouse-accordion">
                {foreach from=$warehouses item=warehouse name=data}
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <a class="accordion-toggle" data-toggle="collapse" data-parent="#warehouse-accordion" href="#warehouse-{$warehouse['id_warehouse']}">{$warehouse['name']}</a>
                        </div>
                        <div id="warehouse-{$warehouse['id_warehouse']}">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th class="fixed-width-xs" align="center"><span class="title_box">{l s='Stored'}</span></th>
                                        <th><span class="title_box">{l s='Product'}</span></th>
                                            {*										<th><span class="title_box">{l s='Location (optional)'}</span></th>*}
                                        <th><span class="title_box">{l s='Shelf (optional)'}</span></th>
                                        <th><span class="title_box">{l s='Bin (optional)'}</span></th>
                                        <th><span class="title_box">{l s='Container (optional)'}</span></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {foreach $attributes AS $index => $attribute}
                                        {assign var=location value=''}
                                        {assign var=selected value=''}
                                        {assign var=selected_shelf value=''}
                                        {assign var=selected_bin value=''}
                                        {assign var=selected_container value=''}
                                        {foreach from=$associated_warehouses item=aw}
                                            {if $aw->id_product == $attribute['id_product'] && $aw->id_product_attribute == $attribute['id_product_attribute'] && $aw->id_warehouse == $warehouse['id_warehouse']}
                                                {assign var=location value=$aw->location}
                                                {assign var=selected_shelf value=$aw->id_shelf}
                                                {assign var=selected_bin value=$aw->id_bin}
                                                {assign var=selected_container value=$aw->id_container}
                                                {assign var=selected value=true}
                                            {/if}
                                        {/foreach}
                                        <tr {if $index is odd}class="alt_row"{/if}>
                                            <td class="fixed-width-xs" align="center"><input type="checkbox"
                                                                                             name="check_warehouse_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}"
                                                                                             {if $selected == true}checked="checked"{/if}
                                                                                             value="1" class ='check_warehouse'/>
                                            </td>
                                            <td>{$product_designation[$attribute['id_product_attribute']]}</td>
                                            {*<td><input type="text"
                                            name="location_warehouse_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}"
                                            value="{$location|escape:'html':'UTF-8'}"
                                            size="20" />
                                            </td>*}
                                            <td>
                                                <select name="shelf_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}" id ="shelf_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}" class ="shelf-list">
                                                    <option value="0">Select a shelf</option>
                                                    {foreach  $shelves as $shelf}
                                                        {if $shelf.id_warehouse== $warehouse['id_warehouse']}
                                                            {if $selected_shelf == $shelf.id_shelf}
                                                                <option value ="{$shelf.id_shelf}" selected="selected" >{$shelf.name}</option>
                                                            {else}
                                                                <option value ="{$shelf.id_shelf}">{$shelf.name}</option>
                                                            {/if}
                                                        {/if}
                                                    {/foreach}
                                                </select>
                                            </td>
                                            <td>
                                                <select name="bin_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}"  id ="bin_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}" class ="bin-list">
                                                    <option value="0">Select a bin</option>
                                                    {foreach $bins as $bin}
                                                        {if $bin.id_shelf==$selected_shelf}
                                                            {if  $selected_bin == $bin.id_bin}
                                                                <option value ="{$bin.id_bin}" selected="selected">{$bin.name}</option>
                                                            {else}
                                                                <option value ="{$bin.id_bin}">{$bin.name}</option>
                                                            {/if}
                                                        {/if}
                                                    {/foreach}
                                                </select>
                                            </td>
                                            <td>
                                                <select name="container_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}" id ="container_{$warehouse['id_warehouse']}_{$attribute['id_product']}_{$attribute['id_product_attribute']}">
                                                    <option value="0">Select a container</option>
                                                    {foreach $containers as $container}
                                                        {if $bin.id_bin==$selected_bin}
                                                            {if $selected_container== $container.id_container}
                                                                <option value ="{$container.id_container}" selected="selected">{$container.name}</option>
                                                            {else}
                                                                <option value ="{$container.id_container}">{$container.name}</option>
                                                            {/if}
                                                        {/if}
                                                    {/foreach}
                                                </select>
                                            </td>
                                        </tr>
                                    {/foreach}								
                            </table>
                            {if $attributes|@count gt 1}
                                <button type="button" class="btn btn-default check_all_warehouse" value="check_warehouse_{$warehouse['id_warehouse']}"><i class="icon-check-sign"></i> {l s='Mark / Unmark all product combinations as stored in this warehouse'}</button>
                                <!--<tr>
                                        <td><input type="checkbox" class="check_all_warehouse" value="check_warehouse_{$warehouse['id_warehouse']}" /></td>
                                        <td colspan="2"><i></i></td>
                                </tr>-->
                            {/if}
                        </div>
                    </div>
                {/foreach}
            </div>
        </div>
        <div class="panel-footer">
            <a href="{$link->getAdminLink('AdminProducts')}" class="btn btn-default"><i class="process-icon-cancel"></i> {l s='Cancel'}</a>
            <button type="submit" name="submitAddproduct" class="btn btn-default pull-right"><i class="process-icon-save"></i> {l s='Save'}</button>
            <button type="submit" name="submitAddproductAndStay" class="btn btn-default pull-right"><i class="process-icon-save"></i> {l s='Save and stay'}</button>
        </div>
    </div>
{/if}
